﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject Radzen.DialogService dialogService
@inject NotificationService notificationService
@inject IState<AddClientState> addClientState
@inject IDispatcher dispatcher

<RadzenSteps>
    <Steps>
        <RadzenStepsItem Text="Credential type">
            <div class="row gy-4">
                @foreach (var credType in CredentialTypes)
                {
                    <div class="col-md-6">
                        <RadzenCard @onclick="() => SelectCredentialType(credType)" class="@(SelectedCredentialType != null && SelectedCredentialType.Name == credType.Name ? "selected selectable" : "selectable")">
                            <div class="row">
                                <div class="col-md-3">
                                    <RadzenImage Path="@credType.PictureUrl" Style="width: 60px" />
                                </div>
                                <div class="col">
                                    <h5>@credType.Name</h5>
                                    <p class="text-muted">@(new MarkupString(credType.ShortDescription))</p>
                                </div>
                            </div>
                        </RadzenCard>
                    </div>
                }
            </div>

            <hr class="hr" />

            @if (SelectedCredentialType != null)
            {
                <div>
                    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Label</RadzenText>
                    <RadzenCheckBox Name="IsDefault" @bind-Value="@settings.IsDefault" TValue="bool"></RadzenCheckBox>
                    <RadzenLabel Text="@Global.IsDefault" Component="IsDefault" Style="margin-left: 8px; vertical-align: middle;" />
                </div>
            }
        </RadzenStepsItem>
        <RadzenStepsItem Text="Create" Disabled="@(SelectedCredentialType == null)">
            @switch (SelectedCredentialType?.Type)
            {
                case UserCredential.PWD:
                    <PasswordCredential CredentialSaved="AddPassword" ></PasswordCredential>
                    break;
                case UserCredential.OTP:
                    <OTPCredential CredentialSaved="AddPassword"></OTPCredential>
                    break;
            }
        </RadzenStepsItem>
    </Steps>
</RadzenSteps>

@code {
    [Parameter]
    public string UserId { get; set; }

    record CredentialType
    {
        public string Name { get; set; } = null!;
        public string ShortDescription { get; set; } = null!;
        public string Type { get; set; } = null!;
        public string PictureUrl { get; set; } = null!;
    }

    record CredentialSettings
    {
        public bool IsDefault { get; set; } = false;
    }

    ICollection<CredentialType> CredentialTypes = new List<CredentialType>
    {
        new CredentialType { Name = Global.Password, ShortDescription = Global.PasswordCredDescription, Type = UserCredential.PWD, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/Password.png" },
        new CredentialType { Name = Global.Otp, ShortDescription = Global.OtpCredDescription, Type = UserCredential.OTP, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/OTP.png" }
    };

    CredentialSettings settings = new CredentialSettings();
    CredentialType? SelectedCredentialType { get; set; } = null;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<AddUserCredentialSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.UserCredentialAdded });
                dialogService.Close();
                StateHasChanged();
            });
        }
    }

    void SelectCredentialType(CredentialType credType)
    {
        SelectedCredentialType = credType;
    }

    void AddPassword(UserCredential cred)
    {
        var credential = new UserCredential
        {
            Id = Guid.NewGuid().ToString(),
            CredentialType = SelectedCredentialType.Type,
            Value = cred.Value
        };
        if(SelectedCredentialType.Type == UserCredential.OTP)
            credential.OTPAlg = cred.OTPAlg;

        dispatcher.Dispatch(new AddUserCredentialAction { IsDefault = settings.IsDefault, Credential = credential, UserId = UserId });
    }
}